<?php

/**
 * @package Bike
 * @subpackage File
 */

/**
 * Class for handling file uploads
 * @author Chrome
 * @package Bike
 * @subpackage File
 */
class Bike_File_Upload {
	
	/**
	 * Upload path
	 * @var string
	 */
	protected $uploadPath = '';
		
	/**
	 * Randomize file name or not
	 * @var boolean
	 */
	protected $randomizeFilename = false;
	
	/**
	 * Allowed extensions
	 * @var array
	 */
	protected $allowedExtensions = array();

	/**
	 * Constructor
	 * @param string $uploadPath
	 * @param array $allowedExtensions
	 * @param boolean $randomizeFilename
	 */
	public function __construct($uploadPath = '', array $allowedExtensions = array(), $randomizeFilename = false){
		$this->setUploadPath($uploadPath);
		$this->setAllowedExtensions($allowedExtensions);
		$this->setRandomizeFilename($randomizeFilename);
	}

	/**
	 * Set upload path
	 * @param string $uploadPath
	 * @return boolean
	 */
	public function setUploadPath($uploadPath = ''){
		if(substr($this->uploadPath, -1) != DIRECTORY_SEPARATOR){
			  $this->uploadPath .= DIRECTORY_SEPARATOR;
		}
		$this->uploadPath = $uploadPath;
		return true;
	}

	/**
	 * Get upload path
	 * @throws Bike_File_Upload_Exception
	 * @return string
	 */
	public function getUploadPath(){
		if(empty($this->uploadPath)){
			throw new Bike_File_Upload_Exception('Upload path is not set!', Bike_File_Upload_Exception::UPLOAD_PATH_IS_NOT_SET);
		}
		return $this->uploadPath;
	}

	/**
	 * On or Off randomize file name
	 * @param boolean $randomizeFilename
	 * @return boolean
	 */
	public function setRandomizeFilename($randomizeFilename){
		$this->randomizeFilename = $randomizeFilename;
		return true;
	}

	/**
	 * Return randomize file name setting
	 * @return boolean
	 */
	public function getRandomizeFilename(){
		return $this->randomizeFilename;
	}

	/**
	 * Set allowed extensions
	 * @param array $allowedExtensions
	 * @return boolean
	 */
	public function setAllowedExtensions(array $allowedExtensions = array()){
		$this->allowedExtensions = $allowedExtensions;
		return true;
	}

	/**
	 * Get allowed extensions
	 * @return array
	 */
	public function getAllowedExtensions(){
		return $this->allowedExtensions;
	}

	/**
	 * Upload fire from giver form field
	 * @param string $fieldName
	 * @throws Bike_File_Upload_Exception
	 * @return string uploaded file name
	 */
	public function upload($fieldName = 'userfile'){
		if(empty($this->uploadPath)){
			throw new Bike_File_Upload_Exception('Upload path is not set!', Bike_File_Upload_Exception::UPLOAD_PATH_IS_NOT_SET);
		}
		if(empty($_FILES[$fieldName])){
			throw new Bike_File_Upload_Exception('No field with file!', Bike_File_Upload_Exception::NO_FIELD_WITH_FILE);
		}
		if(UPLOAD_ERR_OK  != $_FILES[$fieldName]['error']){
			throw new Bike_File_Upload_Exception('Problem with uploading file', $_FILES[$fieldName]['error']);
		}
		$destination = $this->uploadPath;
		$pathInfo = pathinfo($_FILES[$fieldName]['name']);
		if(substr($_FILES[$fieldName]['name'], 0, 1) == '.' || (!empty($this->allowedExtensions) && !in_array($pathInfo['extension'], $this->allowedExtensions))){
			throw new Bike_File_Upload_Exception('Not allowed filename', Bike_File_Upload_Exception::NOT_ALLOWED_FILENAME);
		}
		if($this->randomizeFilename){
			mt_srand();
			$destination .= md5(mt_rand()) . '.' . $pathInfo['extension'];
		}else{
			$destination .= $_FILES[$fieldName]['name'];
		}
		if(!is_uploaded_file($_FILES[$fieldName]['tmp_name'])){
			throw new Bike_File_Upload_Exception($_FILES[$fieldName]['tmp_name'] . ' is not uploaded file!', Bike_File_Upload_Exception::NOT_UPLOADED_FILE);
		}
		if(!move_uploaded_file($_FILES[$fieldName]['tmp_name'], $destination)){
			throw new Bike_File_Upload_Exception('Can not move uploaded file', Bike_File_Upload_Exception::CANT_MOVE_UPLOADED_FILE);
		}
		return $destination;
	}
}
